﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Deanery.Controller.Directories;
using Deanery.Controller.StudyData;

namespace Deanery.View
{
    public partial class DirectoriesDialogForm : Form
    {
        public string table;
        private Guid updateGuid = Guid.Empty;
        private ArrayList comboItems = new ArrayList();
        
        public DirectoriesDialogForm(string tableName, int fieldCount, ArrayList record)
        {
            InitializeComponent();
            FormationControls(tableName, record);
            table = tableName;
            if (record != null)
                updateGuid = new Guid(record[0].ToString());
        }

        
        private void FormationControls(string name, ArrayList record)
        {
            switch (name)
            {
                case "ECTS_Ocinka":
                    {
                        var obj = new ECTSOcinkaController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }

                case "Faculty":
                    {
                        var obj = new FacultyController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Privilege_Group":
                    {
                        var obj = new PrivilegeGroupController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Region":
                    {
                        var obj = new RegionController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Semester":
                    {
                        var obj = new SemesterController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Sex":
                    {
                        var obj = new SexController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Status":
                    {
                        var obj = new StatusController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Type_City":
                    {
                        var obj = new TypeCityController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Type_Contacts":
                    {
                        var obj = new TypeContactsController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Type_Documents":
                    {
                        var obj = new TypeDocumentsController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Type_Education":
                    {
                        var obj = new TypeEducationController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Type_Orders":
                    {
                        var obj = new TypeOrdersController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Type_Payment":
                    {
                        var obj = new TypePaymentController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Type_Privilege":
                    {
                        var obj = new TypePrivilegeController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Type_Street":
                    {
                        var obj = new TypeStreetController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }

                case "Type_Test":
                    {
                        var obj = new TypeTestController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Qualification":
                    {
                        var obj = new QualificationController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Napryam":
                    {
                        var obj = new NapryamController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
                case "Subject":
                    {
                        var obj = new SubjectController();
                        CreatePanel(obj.ColumnName(), obj.ColumnValueType(), record, name);
                        break;
                    }
            }

        }

        private Array FillComboBox(string name, int counter)
        {
            switch (name)
            {
                case "ECTS_Ocinka":
                    {
                        var obj = new ECTSOcinkaController();
                        return obj.GetConectedData();
                    }
                case "Faculty":
                    {
                        var obj = new FacultyController();
                        return obj.GetConectedData();
                    }
                case "Type_Privilege":
                    {
                        var obj = new TypePrivilegeController();
                        return obj.GetConectedData();
                    }
                case "Napryam":
                    {
                        var obj = new NapryamController();
                        switch (counter)
                        {
                            case 1:
                                return obj.GetConectedDataFaculty();
                            case 2:
                                return obj.GetConectedDataQualification();
                            default:
                                return null;
                        }
                    }
                default:
                    return null;
            }
        }

        private void CreatePanel(Array columnName, Array columnValueType, ArrayList record, string tableName)
        {
            //columnName.Length - 2 because we have 2 fields from EDBO
            var lenght = columnName.Length;
            if (tableName == "Status") lenght -= 2;
            //

            var tablePanel = new TableLayoutPanel
                {
                    Name = "tp",
                    Padding = new Padding(10, 10, 4, 5),
                    RowCount = lenght - 1,
                    ColumnCount = 2,
                    GrowStyle = TableLayoutPanelGrowStyle.AddColumns,
                    AutoSize = true,
                    Dock = DockStyle.Left,
                };

            tablePanel.Dock = DockStyle.Top;

            var comboBoxCounter = 0;
            var textBoxCounter = 0;
            
            for (int i = 1; i < lenght; i++)
            {
                var lb = new Label
                    {
                        Text = columnName.GetValue(i).ToString(),
                        AutoSize = true,
                        Dock = DockStyle.Fill,
                        Margin = new Padding(7)
                    };

                tablePanel.Controls.Add(lb);

                //create ComboBox and TextBox 

                if (columnValueType.GetValue(i).ToString() == "guid")
                {
                    var cb = new ComboBox();
                    comboBoxCounter++;
                   
                    cb.Name = "cb" + comboBoxCounter;
                    cb.Height = 90;
                    cb.AutoSize = true;
                    cb.Dock = DockStyle.Fill;
                    cb.DropDownStyle = ComboBoxStyle.DropDownList;
                 
                    cb.DataSource = FillComboBox(tableName, comboBoxCounter);
                    // update - do not work!
                    if (record != null)
                    {
                        comboItems.Add(record[i].ToString());
                        //cb.SelectedValue = Convert.ToString(record[i]);
                    }
                    //
                    tablePanel.Controls.Add(cb);
                }

                else
                {
                    var tb = new TextBox();
                    textBoxCounter++;
                    //update

                    if (record != null)
                    {
                        tb.Text = record[i].ToString();
                    }
                    //
                    tb.Name = "tb" + textBoxCounter;
                    tb.Height = 90;
                    tb.AutoSize = true;
                    tb.Dock = DockStyle.Fill;

                    tablePanel.Controls.Add(tb);
                }
            }

            var rowHeight = tablePanel.GetRowHeights();
            Size = new Size(Width, Height + (lenght - 2) * rowHeight[0]);

            Controls.Add(tablePanel);
        }

        private void DirectoriesDialogForm_Load(object sender, EventArgs e)
        {
            SelectComboBoxesItem();
        }

        private void bntCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private ArrayList GetReccord()
        {
            var reccord = new ArrayList();
            foreach (Control control in Controls)
            {
                var tablePanel = control as TableLayoutPanel;
                if (tablePanel != null)
                    foreach (Control panelControl in tablePanel.Controls)
                    {
                        var tb = panelControl as TextBox;
                        var cb = panelControl as ComboBox;

                        if (tb != null) reccord.Add(tb.Text.Trim(' '));
                        if (cb != null) reccord.Add(cb.Text);
                    }
            }
            return reccord;
        }
        
        private void SelectComboBoxesItem()
        {
            var index = 0;
            foreach (Control control in Controls)
            {
                var tablePanel = control as TableLayoutPanel;
                if (tablePanel != null)
                    foreach (Control panelControl in tablePanel.Controls)
                    {
                        var cb = panelControl as ComboBox;
                        if (cb != null)
                        {
                            if (index < comboItems.Count)
                                cb.SelectedIndex = cb.FindStringExact(comboItems[index].ToString());
                            index++;
                        }
                    }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            var reccord = GetReccord();

            switch (table)
            {
                case "ECTS_Ocinka":
                    {
                        var obj = new ECTSOcinkaController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }

                case "Faculty":
                    {
                        var obj = new FacultyController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Privilege_Group":
                    {
                        var obj = new PrivilegeGroupController();
                         if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Region":
                    {
                        var obj = new RegionController();
                         if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
               case "Semester":
                    {
                        var obj = new SemesterController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
             case "Sex":
                    {
                        var obj = new SexController();
                         if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Status":
                    {
                        var obj = new StatusController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Type_City":
                    {
                        var obj = new TypeCityController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Type_Contacts":
                    {
                        var obj = new TypeContactsController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Type_Documents":
                    {
                        var obj = new TypeDocumentsController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Type_Education":
                    {
                        var obj = new TypeEducationController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Type_Orders":
                    {
                        var obj = new TypeOrdersController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Type_Payment":
                    {
                        var obj = new TypePaymentController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Type_Privilege":
                    {
                        var obj = new TypePrivilegeController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                case "Type_Street":
                    {
                        var obj = new TypeStreetController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }

                case "Type_Test":
                    {
                        var obj = new TypeTestController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }

                case "Qualification":
                    {
                        var obj = new QualificationController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }

                case "Napryam":
                    {
                        var obj = new NapryamController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
                
                case "Subject":
                    {
                        var obj = new SubjectController();
                        if (updateGuid != Guid.Empty)
                        {
                            obj.Update(updateGuid, reccord);
                            break;
                        }
                        obj.Insert(reccord);
                        break;
                    }
            }

            Close();
        }

        
    }
}
